﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace TrangTrai.Data.Utils
{
    public class ConnectionClass
    {
        public static SqlConnection sqlConn = new SqlConnection();
        static String connectionString = "";
        public static bool ConnectToDB()
        {
            getConnectionString();
            try
            {
                sqlConn.ConnectionString = connectionString;
                sqlConn.Open();
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }
        public static void closeConn()
        {
            sqlConn.Close();
        }
        public static string getConnectionString() {
            connectionString = ConfigurationManager.ConnectionStrings["TOM_MSConnectionString"].ConnectionString;
            
            return connectionString;
        }

        public static SqlConnection getConnection()
        {
            
            getConnectionString();
            try
            {
                
                if (sqlConn==null ||sqlConn.State == ConnectionState.Closed) {
                    sqlConn.ConnectionString = connectionString;
                    sqlConn.Open();
                }
            }
            catch (Exception e)
            {
                return null;
            }
            return sqlConn;
        }

    }
}